package ynu.edu.elmspringboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import ynu.edu.elmspringboot.mapper.DeliveryAddressMapper;
import ynu.edu.elmspringboot.po.DeliveryAddress;
import ynu.edu.elmspringboot.service.DeliveryAddressService;

import java.util.List;

@Service
public class DeliveryAddressServiceImpl extends ServiceImpl<DeliveryAddressMapper, DeliveryAddress> implements DeliveryAddressService {

    @Autowired
    private DeliveryAddressMapper deliveryAddressMapper;

    @Override
    public IPage<DeliveryAddress> getDeliveryAddressPage(Page<DeliveryAddress> page, String userId, String contactName) {
        QueryWrapper<DeliveryAddress> queryWrapper = new QueryWrapper<>();
        
        // 添加用户ID查询条件
        if (StringUtils.hasText(userId)) {
            queryWrapper.eq("user_id", userId);
        }
        
        // 添加联系人姓名模糊查询条件
        if (StringUtils.hasText(contactName)) {
            queryWrapper.like("contact_name", contactName);
        }
        
        // 按地址ID排序
        queryWrapper.orderByDesc("da_id");
        
        return this.page(page, queryWrapper);
    }

    @Override
    public IPage<DeliveryAddress> getDeliveryAddressPageByUser(Page<DeliveryAddress> page, String userId) {
        QueryWrapper<DeliveryAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.orderByDesc("da_id");
        
        return this.page(page, queryWrapper);
    }
}
